package com.supermap.geometrist.toolkit.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.supermap.data.GeoLine;
import com.supermap.data.GeoPoint;
import com.supermap.data.GeoRegion;
import com.supermap.data.Geometry;
import com.supermap.data.Point2D;
import com.supermap.data.Point2Ds;

public class GeometristTools {
    public Geometry analysis(JSONObject params) {
        String type = params.getString("type");
        if (type == null) {
            return null;
        }
        JSONArray pointArray = params.getJSONArray("points");
        switch (type) {

            case "POINT": {
                if (pointArray.size() == 1) {
                    JSONObject point = pointArray.getJSONObject(0);
                    GeoPoint geopoint = new GeoPoint();
                    geopoint = new GeoPoint(new Point2D(point.getDouble("x"), point.getDouble("y")));
                    return geopoint;
                }
                break;
            }

            case "LINE": {
                if (pointArray.size() > 1) {
                    Point2Ds points = new Point2Ds();
                    for (int i = 0; i < pointArray.size(); i++) {
                        JSONObject point = pointArray.getJSONObject(i);
                        points.add(new Point2D(point.getDouble("x"), point.getDouble("y")));
                    }
                    GeoLine line = new GeoLine(points);
                    return line;
                }
                break;
            }

            case "REGION": {
                if (pointArray.size() > 2) {
                    Point2Ds points = new Point2Ds();
                    for (int i = 0; i < pointArray.size(); i++) {
                        JSONObject point = pointArray.getJSONObject(i);
                        points.add(new Point2D(point.getDouble("x"), point.getDouble("y")));
                    }
                    GeoRegion region = new GeoRegion(points);
                    return region;
                }
                break;
            }
            default: {
                return null;
            }
        }
        return null;
    }

}
